package br.cnpq.webflowah.controle;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
//import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.table.DefaultTableModel;

import org.mindswap.owl.OWLIndividual;
import org.mindswap.owls.service.Service;

import br.cnpq.webflowah.beans.ServicoSemantico;
import br.cnpq.webflowah.gui.TelaClassificarConceito;
import br.cnpq.webflowah.gui.TelaImportarOWLS;
import br.cnpq.webflowah.ontologia.ServicoOWL;

public class GestorServico {

	public GestorServico() {
	}

	public void doAbrir(JDialog view, DefaultTableModel model) {
		try {
			List list = ServicoOWL.recuperarTodos();
			// FIXME: Thiago - Ordenar a lista para facilitar a visualizacao
			Collections.sort(list, new Comparator<ServicoSemantico>() {
				@Override
				public int compare(ServicoSemantico o1, ServicoSemantico o2) {
					return o1.getNome().compareTo(o2.getNome());
				}
			});

			for (int i = 0; list != null && i < list.size(); i++) {
				// Vector rowData = (Vector) list.get(i);
				// model.addRow(rowData);
				ServicoSemantico ss = (ServicoSemantico) list.get(i);
				model.addRow(ss.toArray());
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(view, e.getMessage(), "Mensagem",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	public void doAdicionar(JDialog view, DefaultTableModel model) {
		ListModel list = TelaImportarOWLS.obterServicos(view);

		for (int i = 0; list != null && i < list.getSize(); i++) {
			OWLIndividual ind = (OWLIndividual) list.getElementAt(i);
			Service service = ind != null ? (Service) ind.castTo(Service.class)
					: null;

			if (service != null) {
				// Vector<String> rowData = new Vector<String>();
				// String uri = service.getURI().toString();
				// String categoria =
				// service.getProfile().getType().getLocalName();
				// rowData.add(service.getLocalName());
				// rowData.add(categoria);
				// rowData.add(uri.substring(0, uri.lastIndexOf("#")));
				// model.addRow(rowData);

				String uri = service.getURI().toString();
				String categoria = (service.getProfile().getType() != null) ? service
						.getProfile().getType().getLocalName()
						: "Indefinida";
				categoria = categoria.equals("Profile") ? "Indefinida"
						: categoria;
				ServicoSemantico ss = new ServicoSemantico();
				ss.setURI(uri);
				ss.setNome(service.getLocalName());
				ss.setCategoria(categoria);
				ss.setOntologia(uri.substring(0, uri.lastIndexOf("#")));

				if (!ServicoOWL.existir(uri)) {
					ServicoOWL.salvar(ss);
					model.addRow(ss.toArray());

					int op = JOptionPane.showConfirmDialog(view,
							"Deseja classificar os conceitos deste servi�o \n"
									+ "nas ontologias de dom�nio comum?",
							"Confirma��o", JOptionPane.YES_NO_OPTION,
							JOptionPane.QUESTION_MESSAGE);

					if (op == JOptionPane.OK_OPTION)
						doClassificar(view, model, model.getRowCount() - 1);
				} else {
					JOptionPane.showConfirmDialog(view,
							"Este servi�o j� foi importado! Tente remov�-lo primeiro \n"
									+ "caso deseja import�-lo novamente.",
							"Mensagem", JOptionPane.CLOSED_OPTION,
							JOptionPane.WARNING_MESSAGE);
				}
			}
		}
	}

	public void doClassificar(JDialog view, DefaultTableModel model, int row) {
		if (row < 0)
			return;
		String uri = "" + model.getValueAt(row, 2) + "#"
				+ model.getValueAt(row, 0);
		ServicoSemantico servico = ServicoOWL.recuperar(uri);
		TelaClassificarConceito.classificarConceitos(view, servico);
	}

	public void doFechar(JDialog view, DefaultTableModel model) {
		try {
			// ServicoOWL.salvarTodos(model.getDataVector());
			ServicoOWL.commit();
			view.dispose();
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(view, e.getMessage(), "Mensagem",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	public void doRemover(JDialog view, DefaultTableModel model, int row) {
		if (row < 0)
			return;

		int opt = JOptionPane.showConfirmDialog(view,
				"Deseja remover servi�o Web sem�ntico?", "Confirma��o",
				JOptionPane.YES_NO_OPTION);

		if (opt == JOptionPane.YES_OPTION) {
			try {
				String uri = "" + model.getValueAt(row, 2) + "#"
						+ model.getValueAt(row, 0);
				ServicoSemantico servico = ServicoOWL.recuperar(uri);
				ServicoOWL.apagar(servico);

				model.removeRow(row);
			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(view, e.getMessage(), "Mensagem",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
